package com.moxiao.yfjh.module.blind.service.constellationspeech;

import com.moxiao.yfjh.module.blind.common.BlindNotifyMessageUtils;
import com.moxiao.yfjh.module.blind.controller.admin.constellationtest.vo.ConstellationTestUpdateReqVO;
import com.moxiao.yfjh.module.blind.dal.dataobject.constellationtest.ConstellationTestDO;
import com.moxiao.yfjh.module.blind.service.constellationtest.ConstellationTestService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.constellationspeech.vo.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.constellationspeech.ConstellationSpeechDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.constellationspeech.ConstellationSpeechConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.constellationspeech.ConstellationSpeechMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员录音回复 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class ConstellationSpeechServiceImpl implements ConstellationSpeechService {

    @Resource
    private ConstellationSpeechMapper constellationSpeechMapper;
    @Resource
    private BlindNotifyMessageUtils blindNotifyMessageUtils;
    @Resource
    private ConstellationTestService constellationTestService;

    @Override
    public Long createConstellationSpeech(ConstellationSpeechCreateReqVO createReqVO) {
        // 插入
        ConstellationSpeechDO constellationSpeech = ConstellationSpeechConvert.INSTANCE.convert(createReqVO);
        constellationSpeechMapper.insert(constellationSpeech);
        // 返回
        return constellationSpeech.getId();
    }

    @Override
    public void createConstellationSpeech(List<ConstellationSpeechCreateReqVO> createReqVO) {

        List<ConstellationSpeechDO> constellationSpeechDOS = ConstellationSpeechConvert.INSTANCE.convert(createReqVO);
        constellationSpeechMapper.insertBatch(constellationSpeechDOS);
    }

    @Override
    @Transactional
    public void createConstellation(List<ConstellationSpeechCreateReqVO> createReqVO) {
        createConstellationSpeech(createReqVO);
        blindNotifyMessageUtils.createSpeechMessage(createReqVO.get(0).getUserId());
        ConstellationTestDO constellationTest = constellationTestService.getConstellationTest(createReqVO.get(0).getTestId());
        if (Objects.nonNull(constellationTest)) {
            ConstellationTestUpdateReqVO updateReqVO = new ConstellationTestUpdateReqVO();
            updateReqVO.setId(constellationTest.getId());
            updateReqVO.setStatus((byte) 1);
            constellationTestService.updateConstellationTest(updateReqVO);
        }
    }

    @Override
    public void updateConstellationSpeech(ConstellationSpeechUpdateReqVO updateReqVO) {
        // 校验存在
        validateConstellationSpeechExists(updateReqVO.getId());
        // 更新
        ConstellationSpeechDO updateObj = ConstellationSpeechConvert.INSTANCE.convert(updateReqVO);
        constellationSpeechMapper.updateById(updateObj);
    }

    @Override
    public void deleteConstellationSpeech(Long id) {
        // 校验存在
        validateConstellationSpeechExists(id);
        // 删除
        constellationSpeechMapper.deleteById(id);
    }

    private void validateConstellationSpeechExists(Long id) {
        if (constellationSpeechMapper.selectById(id) == null) {
            throw exception(CONSTELLATION_SPEECH_NOT_EXISTS);
        }
    }

    @Override
    public ConstellationSpeechDO getConstellationSpeech(Long id) {
        return constellationSpeechMapper.selectById(id);
    }

    @Override
    public List<ConstellationSpeechDO> getConstellationSpeechList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return constellationSpeechMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ConstellationSpeechDO> getConstellationSpeechPage(ConstellationSpeechPageReqVO pageReqVO) {
        return constellationSpeechMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ConstellationSpeechDO> getConstellationSpeechList(ConstellationSpeechExportReqVO exportReqVO) {
        return constellationSpeechMapper.selectList(exportReqVO);
    }

}
